<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
    const threeSum = function(nums) {
        const len = nums.length
        if(len<3){
            return []
        }
        nums = nums.sort((a,b)=>{return a-b})
        const result = []
        for(let i = 0;i<len-2;i++){
            if(nums[i]>0) return;
            let l = i+1;let r = len-1
            while(l<r){
                const item = []
                if(nums[i]+nums[l]+nums[r]>0){
                    r--
                }else if(nums[i]+nums[l]+nums[r]<0){
                    l++
                }else{
                    item.push(nums[i],nums[l],nums[r])
                    result.push(item)
                    r--
                    l++
                }
            }
        }
        // result 是初步的结果，但是还是要去重
        const set = new Set(result)
        console.log(Array.from(set))
        return result
    };
    threeSum([-1,0,1,2,-1,-4])
</script>
</body>
</html>
